<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <meta name="keywords" content="" />
    <meta name="description" content="">
    <link href="css/bulma.min.css" rel="stylesheet">
    <link href="css/github-markdown.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/app.css">
    <script src="./js/config.js"></script>
    <script src="./js/jquery.min.js"></script>
    <script src="./js/urlutil.js"></script>
</head>

<body>
    <div>
        <header class="editor-header">
            <input id="title" name="title" value="标题" class="input is-shadowless is-radiusless" type="text"
                placeholder="输入文章标题...">
            <div class="right-box">
                <div class="select is-small">
                    <select id="category">
                        <option disabled="disabled">分类</option>
                    </select>
                </div>
                <button type="submit" class="button is-small is-primary" onclick="updateArt()">更新</button>
            </div>
        </header>
        <div id="editor">
            <textarea autofocus="autofocus" name="content" class="input" name="content">正文</textarea>
            <div class="show content markdown-body"></div>
        </div>
    </div>
    <nav class="navbar has-shadow">
        <div class="navbar-brand">
            <a class="navbar-item" href="index.html">
                返回
            </a>
        </div>
    </nav>
    <script src="js/marked.min.js"></script>
    <script>
        var input = $('#editor .input');
        $('#editor .show').html(marked(input.val()));
        input.on('input', function () {
            $('#editor .show').html(marked($(this).val(), { sanitize: true }))
        })
    </script>
</body>

<script>
    // 去加载文章的分类
    function initCategory() {
        jQuery.ajax({
            url: base_url + "/category/list",
            method: "GET",
            data: {
            },
            // 携带 token 进行是否登录判断
            headers: {
                "Authorization": window.localStorage.getItem(token_key)
            },
            success: function (result) {
                if (result != null && result.code == 200) {
                    // 获取文章分类是一个 List 集合
                    var ListHtml = "";
                    for (let i = 0; i < result.data.length; i++) {
                        // 获取单个对象
                        var category = result.data[i];
                        ListHtml += '<option value="' + category.cid + '">' + category.name + '</option>';
                    }
                    jQuery("#category").append(ListHtml);
                    return;
                } else {
                    alert("获取文章分类失败！" + result.msg);
                    return;
                }
            }
        });
    }
    initCategory();

    // url 中拿到文章的 aid 进行展示
    var aid = getUrlParam("aid");

    function initArticleInfo() {
        // 验证参数
        if (aid == null || aid <= 0) {
            alert("获取文章参数有误！");
            return;
        }
        jQuery.ajax({
            url: base_url + "/aritcle/getbyId",
            method: "POST",
            data: {
                "aid": aid
            },
            // 携带 token 进行是否登录判断
            headers: {
                "Authorization": window.localStorage.getItem(token_key)
            },
            success: function (result) {
                if (result != null && result.code == 200 && result.data != null) {
                    jQuery("#category").val(result.data.cid);
                    jQuery("#title").val(result.data.title);
                    input.val(result.data.content); // 添加正文
                    $('#editor .show').html(marked(result.data.content));
                    return;
                } else {
                    alert("获取文章信息失败！" + result.msg);
                    return;
                }
            }
        });
    }
    initArticleInfo();


    // 文章更新操作
    function updateArt() {
        //1、 验证参数
        var title = jQuery("#title");
        if (title.val() == "") {
            alert("请输入文章标题!");
            title.focus();
            return;
        }

        if (input.val() == "") {
            alert("请输入文章正文!");
            return;
        }

        var cid = jQuery("#category").val();
        if (cid == null || cid == "" || cid == "0") {
            alert("请选择要更新文章的类型!");
            return;
        }

        jQuery.ajax({
            url: base_url + "/aritcle/update",
            method: "POST",
            data: {
                "aid": aid,
                "title": title.val(),
                "content": input.val(),
                "cid": cid
            },
            // 携带 token 进行是否登录判断
            headers: {
                "Authorization": window.localStorage.getItem(token_key)
            },
            success: function (result) {
                if (result != null && result.code == 200 && result.data == true) {
                    alert("恭喜：更新文章成功！");
                    return;
                } else {
                    alert("很遗憾：更新文章失败！" + result.msg);
                    return;
                }
            }
        });
    }

</script>

</html>